package com.dkd.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manage.domain.Strategy;
import com.dkd.manage.service.IStrategyService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;

/**
 * 折扣策略Controller
 *
 * @author ruoyi
 * @date 2025-11-10
 */
@RestController
@RequestMapping("/manage/strategy")
public class StrategyController extends BaseController
{
    @Autowired
    private IStrategyService strategyService;

    /**
     * 查询折扣策略列表
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:list')")
    @GetMapping("/list")
    public TableDataInfo list(Strategy strategy)
    {
        startPage();
        List<Strategy> list = strategyService.selectStrategyList(strategy);
        return getDataTable(list);
    }

    /**
     * 查询有效的策略列表
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:list')")
    @GetMapping("/activeList")
    public AjaxResult activeList(Strategy strategy)
    {
        List<Strategy> list = strategyService.selectActiveStrategyList(strategy);
        return AjaxResult.success(list);
    }

    /**
     * 根据设备ID查询适用的策略
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:list')")
    @GetMapping("/device/{deviceId}")
    public AjaxResult getStrategiesByDeviceId(@PathVariable("deviceId") Long deviceId)
    {
        List<Strategy> list = strategyService.selectStrategiesByDeviceId(deviceId);
        return AjaxResult.success(list);
    }

    /**
     * 导出折扣策略列表
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:export')")
    @Log(title = "折扣策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Strategy strategy)
    {
        List<Strategy> list = strategyService.selectStrategyList(strategy);
        ExcelUtil<Strategy> util = new ExcelUtil<Strategy>(Strategy.class);
        util.exportExcel(response, list, "折扣策略数据");
    }

    /**
     * 获取折扣策略详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(strategyService.selectStrategyById(id));
    }

    /**
     * 新增折扣策略
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:add')")
    @Log(title = "折扣策略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Strategy strategy)
    {
        return toAjax(strategyService.insertStrategy(strategy));
    }

    /**
     * 修改折扣策略
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:edit')")
    @Log(title = "折扣策略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Strategy strategy)
    {
        return toAjax(strategyService.updateStrategy(strategy));
    }

    /**
     * 删除折扣策略
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:remove')")
    @Log(title = "折扣策略", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(strategyService.deleteStrategyByIds(ids));
    }

    /**
     * 分配策略给设备
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:assign')")
    @Log(title = "策略分配", businessType = BusinessType.UPDATE)
    @PostMapping("/assignDevices")
    public AjaxResult assignDevices(@RequestBody StrategyDeviceAssignRequest request)
    {
        return toAjax(strategyService.assignDevicesToStrategy(
            request.getStrategyId(),
            request.getDeviceIds(),
            getUsername()
        ));
    }

    /**
     * 取消策略与设备的关联
     */
    @PreAuthorize("@ss.hasPermi('manage:strategy:unassign')")
    @Log(title = "策略取消分配", businessType = BusinessType.UPDATE)
    @PostMapping("/unassignDevices")
    public AjaxResult unassignDevices(@RequestBody StrategyDeviceAssignRequest request)
    {
        return toAjax(strategyService.unassignDevicesFromStrategy(
            request.getStrategyId(),
            request.getDeviceIds()
        ));
    }

    /**
     * 策略设备分配请求对象
     */
    public static class StrategyDeviceAssignRequest
    {
        private Long strategyId;
        private Long[] deviceIds;

        public Long getStrategyId() {
            return strategyId;
        }

        public void setStrategyId(Long strategyId) {
            this.strategyId = strategyId;
        }

        public Long[] getDeviceIds() {
            return deviceIds;
        }

        public void setDeviceIds(Long[] deviceIds) {
            this.deviceIds = deviceIds;
        }
    }
}